library(ggplot2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ lubridate 1.9.3 ✔ tibble 3.2.1
## ✔ purrr 1.0.2 ✔ tidyr 1.3.1
## ✔ readr 2.1.5
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ plotly::filter() masks dplyr::filter(), stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
knitr::opts_chunk$set(
fig.width = 6,
fig.asp = .6,
out.width = "90%"
)
theme_set(theme_minimal() + theme(legend.position = "bottom"))
options(
ggplot2.continuous.colour = "viridis",
ggplot2.continuous.fill = "viridis"
)
scale_colour_discrete = scale_colour_viridis_d
scale_fill_discrete = scale_fill_viridis_d
##Load Dataset
spotify_data =
read_csv("spotify_songs.csv") |>
mutate(popularity=track_popularity,
genre=playlist_genre)
## Rows: 32833 Columns: 23
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (10): track_id, track_name, track_artist, track_album_id, track_album_na...
## dbl (13): track_popularity, danceability, energy, key, loudness, mode, speec...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
##Distrubution of dancebility
violin_danceability = spotify_data |>
plot_ly(
x = ~genre,
y = ~danceability,
type = "violin",
color= ~genre,
box = list(visible = TRUE),
meanline = list(visible = TRUE)
) |>
layout(
title = "Distribution of Danceability by Genre",
xaxis = list(title = "Genre"),
yaxis = list(title = "Danceability")
)
violin_danceability
##Danceability and popularity
scatter_plot = spotify_data |>
plot_ly(
x = ~danceability,
y = ~popularity,
color= ~genre,
type = "scatter",
alpha=.2,
text = ~paste("Genre: ", genre, "<br>Danceability: ", danceability, "<br>Popularity: ", popularity)
) |>
layout(
title = "Danceability vs. Popularity",
xaxis = list(title = "Danceability", range = c(0, 1)),
yaxis = list(title = "Popularity", range = c(0, 100)),
showlegend = FALSE
)
scatter_plot
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
model = lm(popularity ~ danceability, data = spotify_data)
scatter_plot = spotify_data |>
plot_ly(
x = ~danceability,
y = ~popularity,
color = ~genre,
type = "scatter",
alpha = 0.2,
text = ~paste("Genre: ", genre, "<br>Danceability: ", danceability, "<br>Popularity: ", popularity)
) |>
add_trace(
x = ~spotify_data$danceability,
y = ~predict(model),
mode = "lines",
line = list(color = "red"),
name = "Regression Line"
) |>
layout(
title = "Danceability vs. Popularity",
xaxis = list(title = "Danceability", range = c(0, 1)),
yaxis = list(title = "Popularity", range = c(0, 100)),
showlegend = FALSE
)
scatter_plot
## No scatter mode specifed:
## Setting the mode to markers
## Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode